"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
    return { next: verb(0), "throw": verb(1), "return": verb(2) };
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [0, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var common = require("../../../common");
module.exports = function (app) {
    return new Handler(app);
};
var Handler = function (app) {
    this.app = app;
};
var handler = Handler.prototype;
/**
 *
 * @see  判断前端传入的参数是否正确  如果有继续下一步
 */
// ************************************ Request
/**
 * @see  xxxx
 */
handler.matching = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["mode", "maxUserCount"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            //增加index  字段 ==用户选择模式的索引
            this.app.rpc.room.roomRemote.matching(session, session.uid, msg.mode, msg.index, msg.maxUserCount, function (roomNumber) {
                var state = roomNumber >= 0 ? 1 : 2;
                common.sendResult(next, { state: state, roomNumber: roomNumber });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.create = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["mode", "maxUserCount", "password"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.create(session, session.uid, msg.mode, msg.index, msg.maxUserCount, msg.password, function (roomNumber) {
                var state = roomNumber >= 0 ? 1 : 2;
                common.sendResult(next, { state: state, roomNumber: roomNumber });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.createLive = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["mode", "maxUserCount", "title"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.createLive(session, session.uid, msg.mode, msg.index, msg.maxUserCount, msg.password, function (roomNumber) {
                var state = roomNumber >= 0 ? 1 : 2;
                common.sendResult(next, { state: state, roomNumber: roomNumber });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.enter = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomNumber", "password"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.enter(session, session.uid, msg.roomNumber, msg.password, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  女巫使用解药 or 人物按钮点击
 */
handler.updateSelects = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "selects"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.updateSelects(session, msg.roomId, session.uid, msg.selects, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  取消密码
 */
handler.changePassword = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "password"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.changePassword(session, msg.roomId, session.uid, msg.password, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
// ************************************ Notify
/**
 * @see  xxxx
 */
handler.leave = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.leave(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  准备(取消准备)游戏
 */
handler.ready = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "isReady"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.ready(session, msg.roomId, session.uid, msg.isReady, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  开始游戏
 */
handler.start = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.start(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.addRobot = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.addRobot(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  发送文字聊天信息
 */
handler.sendMessage = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "message"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.sendMessage(session, msg.roomId, session.uid, msg.message, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  狼人杀人
 */
handler.sendWolfMessage = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "number", "type"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.sendWolfMessage(session, msg.roomId, session.uid, msg.type, msg.number, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.sendBullet = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "message"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.sendBullet(session, msg.roomId, session.uid, msg.message, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see   结束当前人的音视频
          跳过下一步
 */
handler.stopSpeak = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.stopSpeak(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  放弃上警/ 退水
 */
handler.withdrawn = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.withdrawn(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  猎人是否发动技能
 */
handler.hunterGun = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "gun"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.hunterGun(session, msg.roomId, session.uid, msg.gun, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  踢出玩家
 */
handler.kickPlayer = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "playerId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.kickPlayer(session, msg.roomId, session.uid, msg.playerId, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  邀请朋友
 */
handler.inviteUsers = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "userIds"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.inviteUsers(session, session.uid, msg.roomId, msg.userIds, next);
            return [2 /*return*/];
        });
    });
};
/**
 * 跳过下一个回合
 */
handler.stopWait = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.stopWait(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  绑定视频成员ID
 */
handler.bindMemberId = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "memberId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.bindMemberId(session, msg.roomId, session.uid, msg.memberId, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  礼物界面
 */
handler.giveItem = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "playerId", "itemId", "isCombo", "isBuy"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.giveItem(session, msg.roomId, session.uid, msg.playerId, msg.itemId, msg.isBuy, msg.isCombo, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  礼物界面   实物
 */
handler.giveRealItem = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "playerId", "itemId", "isCombo", "isBuy", "isRealGoods"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.giveRealItem(session, msg.roomId, session.uid, msg.playerId, msg.itemId, msg.isBuy, msg.isCombo, msg.isRealGoods, 
            // msg.addressInfo,
            next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.doViewer = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.doViewer(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  xxxx
 */
handler.doPlayer = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.doPlayer(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  关闭直播(只有主播有权利)
 */
handler.close = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.close(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
handler.getAnchorRoomInfos = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            this.app.rpc.room.roomRemote.getAnchorRoomInfos(session, session.uid, function (infos) {
                common.sendResult(next, { infos: infos });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  买座位
 */
handler.buySeat = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "price", "index"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.buySeat(session, msg.roomId, session.uid, msg.price, msg.index, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  狼人自曝
 */
handler.exposure = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.exposure(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
// handler.changeSeat = async function(msg, session, next) {
// 	let msgErr = common.getMsgError(msg, ["roomId", "index"]);
// 	if (msgErr) { console.warn(msgErr); next(); return; }
// 	this.app.rpc.room.roomRemote.changeSeat(session, msg.roomId, session.uid, session.index, next);
// };
handler.changeSeat = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId", "seatIndex"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.changeSeat(session, msg.playerId, msg.roomId, session.uid, msg.seatIndex, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see 玩家点击跳过当前步骤
 */
handler.skipCurrentStep = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            // this.app.rpc.room.roomRemote.skipCurrentStep(session, msg.roomId, session.uid,  msg.desc,msg.tableIndex,next);
            this.app.rpc.room.roomRemote.skipCurrentStep2(session, msg.roomId, session.uid, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  白痴玩家点击  跳过插话阶段
 */
handler.skipFoolSpeaking = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["roomId"]);
            if (msgErr) {
                console.warn(msgErr);
                next();
                return [2 /*return*/];
            }
            this.app.rpc.room.roomRemote.skipFoolSpeaking(session, msg, next);
            return [2 /*return*/];
        });
    });
};
